<template>
	<div class="app-container">
		<!--    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			  <el-form-item label="名称" prop="name">
				<el-input
				  v-model="queryParams.name"
				  placeholder="请输入名称"
				  clearable
				  size="small"
				  @keyup.enter.native="handleQuery"
				/>
			  </el-form-item>
			  <el-form-item label="缩略图" prop="thumbnail">
				<el-input
				  v-model="queryParams.thumbnail"
				  placeholder="请输入缩略图"
				  clearable
				  size="small"
				  @keyup.enter.native="handleQuery"
				/>
			  </el-form-item>
			  <el-form-item label="父节点" prop="parentId">
				<el-input
				  v-model="queryParams.parentId"
				  placeholder="请输入父id"
				  clearable
				  size="small"
				  @keyup.enter.native="handleQuery"
				/>
			  </el-form-item>
			  <el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			  </el-form-item>
			</el-form>-->

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
				>新增
				</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table
			v-loading="loading"
			:data="menuList"
			row-key="menuId"
			default-expand-all
			:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
		>
			<el-table-column label="名称" align="center" prop="name"/>
			<el-table-column label="缩略图" align="center" prop="thumbnail">
				<template slot-scope="scope">
					<el-image v-if="scope.row.thumbnail" class="image" :src="ossUrl + scope.row.thumbnail"></el-image>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						@click="handleUpdate(scope.row)"
					>修改
					</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
					>删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 添加或修改menu对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称"/>
				</el-form-item>
				<el-form-item label="缩略图" prop="thumbnail">
					<el-upload
						class="video_uploader"
						:action="upload.url"
						:headers="upload.headers"
						:show-file-list="false"
						:on-success="handleImageSuccess"
						:before-upload="beforeImageUpload">
						<img v-if="form.thumbnail" :src="ossUrl + form.thumbnail" class="avatar" alt="">
						<i v-else class="el-icon-plus video_uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="父节点" prop="parentId">
					<treeselect v-model="form.parentId" :options="menuOptions" :normalizer="normalizer"
								placeholder="请选择父id"/>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {listMenu, getMenu, delMenu, addMenu, updateMenu, exportMenu} from "@/api/hospital/menu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getToken} from "@/utils/auth";

export default {
	name: "Menu",
	components: {Treeselect},
	data() {
		return {
			// 遮罩层
			loading: true,
			// 显示搜索条件
			showSearch: true,
			// menu表格数据
			menuList: [],
			// menu树选项
			menuOptions: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				name: null,
				thumbnail: null,
				parentId: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
			upload: {
				// 是否禁用上传
				isUploading: false,
				// 设置上传的请求头部
				headers: {Authorization: "Bearer " + getToken()},
				// 上传的地址
				url: process.env.VUE_APP_BASE_API + "/uploadOssFile",
			},
			ossUrl: this.$ossUrl,
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询menu列表 */
		getList() {
			this.loading = true;
			listMenu(this.queryParams).then(response => {
				this.menuList = this.handleTree(response.data, "menuId", "parentId");
				this.loading = false;
			});
		},
		/** 转换menu数据结构 */
		normalizer(node) {
			if (node.children && !node.children.length) {
				delete node.children;
			}
			return {
				id: node.menuId,
				label: node.name,
				children: node.children
			};
		},
		/** 查询部门下拉树结构 */
		getTreeselect() {
			listMenu().then(response => {
				this.menuOptions = [];
				const data = {menuId: 0, name: '顶级节点', children: []};
				data.children = this.handleTree(response.data, "menuId", "parentId");
				this.menuOptions.push(data);
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				menuId: null,
				name: null,
				thumbnail: null,
				parentId: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.getTreeselect();
			this.open = true;
			this.title = "添加资源菜单";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			this.getTreeselect();
			if (row != null) {
				this.form.parentId = row.menuId;
			}
			getMenu(row.menuId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改menu";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.menuId != null) {
						updateMenu(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							}
						});
					} else {
						addMenu(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							}
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			this.$confirm('是否确认删除menu编号为"' + row.menuId + '"的数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return delMenu(row.menuId);
			}).then(() => {
				this.getList();
				this.msgSuccess("删除成功");
			}).catch(function () {
			});
		},
		handleImageSuccess(res) {
			this.form.thumbnail = res.fileName;
			if (this.uploadLoading) {
				this.uploadLoading.close();
			}
		},
		beforeImageUpload(file) {
			let videoTypes = ["image/bmp", "image/gif", "image/jpg", "image/jpeg", "image/png"];
			if (!videoTypes.includes(file.type)) {
				this.msgInfo('上传视频只能是 bmp/gif/jpg/jpeg/png 格式!');
				return false;
			}
			this.uploadLoading = this.$loading({
				lock: true,
				text: '加载中'
			});
			return true;
		}
	}
};
</script>

<style scoped>
.video_uploader ::v-deep.el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.video_uploader ::v-deep.el-upload:hover {
	border-color: #409EFF;
}
.video_uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}

.image {
	height: 20px;
	width: 20px;
}
</style>
